/**
* 显示消息提示框
* @param content 提示的标题
*/
export function toast(content) {
  uni.showToast({
    icon: 'none',
    title: content
  })
}

/**
* 显示模态弹窗
* @param content 提示的标题
*/
export function showConfirm(content) {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: '提示',
      content: content,
      cancelText: '取消',
      confirmText: '确定',
      success: function(res) {
        resolve(res)
      }
    })
  })
}

/**
* 参数处理
* @param params 参数
*/
export function tansParams(params) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName]
    var part = encodeURIComponent(propName) + "="
    if (value !== null && value !== "" && typeof (value) !== "undefined") {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
            let params = propName + '[' + key + ']'
            var subPart = encodeURIComponent(params) + "="
            result += subPart + encodeURIComponent(value[key]) + "&"
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&"
      }
    }
  }
  return result
}

// http字符串转数组
export function httpStrToArr(str){
	if(!str){
		return []
	}
	if(str.indexOf('[') == -1 || str.indexOf(']') == -1){
		let arr = str.split(',');
		return arr
	}else{
		if(str == '[]'){
			return []
		}
		let arr = str.split(/[\[\],]/)
		arr.pop()
		arr.shift()
		return arr
	}
}

// 组合树
export const handleTree = (data, id, parentId, children) => {
  if (!Array.isArray(data)) {
    console.warn('data must be an array')
    return []
  }
  const config = {
    id: id || 'id',
    parentId: parentId || 'parentId',
    childrenList: children || 'children'
  }

  const childrenListMap = {}
  const nodeIds = {}
  const tree = []

  for (const d of data) {
    const parentId = d[config.parentId]
    if (childrenListMap[parentId] == null) {
      childrenListMap[parentId] = []
    }
    nodeIds[d[config.id]] = d
    childrenListMap[parentId].push(d)
  }

  for (const d of data) {
    const parentId = d[config.parentId]
    if (nodeIds[parentId] == null) {
      tree.push(d)
    }
  }

  for (const t of tree) {
    adaptToChildrenList(t)
  }

  function adaptToChildrenList(o) {
    if (childrenListMap[o[config.id]] !== null) {
      o[config.childrenList] = childrenListMap[o[config.id]]
    }
    if (o[config.childrenList]) {
      for (const c of o[config.childrenList]) {
        adaptToChildrenList(c)
      }
    }
  }

  return tree
}



// 根据类型不同跳转不同详情
export function jumpTo(str,id){
	if(str && str.indexOf('oa_leave_clear') >= 0){
		// 销假
		uni.navigateTo({
			url:'/pages/process/leaveclear/detial?id='+id
		})
	}else if(str && str.indexOf('oa_leave') >= 0){
		// 跳转请假
		uni.navigateTo({
			url:'/pages/process/leave/detial?id='+id
		})
	}else if(str && str.indexOf('oa_overtime') >= 0){
		// 加班
		uni.navigateTo({
			url:'/pages/process/overtime/detial?id='+id
		})
	}else if(str && str.indexOf('oa_vehicleApply') >= 0){
		// 用车
		uni.navigateTo({
			url:'/pages/process/useCar/detial?id='+id
		})
	}else if(str && str.indexOf('oa_document_circulation') >= 0){
		// 文件传阅
		uni.navigateTo({
			url:'/pages/process/doccirculate/detial?id='+id
		})
	}else if(str && str.indexOf('oa_inventory') >= 0){
		// 应急保障物资
		uni.navigateTo({
			url:'/pages/process/inventory/detial?id='+id
		})
	}else if(str && str.indexOf('oa_publicity_column') >= 0){
		// 公示栏申请
		uni.navigateTo({
			url:'/pages/process/column/detial?id='+id
		})
	}else if(str && str.indexOf('apply_with_seal') >= 0){
		// 用章申请
		uni.navigateTo({
			url:'/pages/process/useSeal/detial?id='+id
		})
	}else if(str && str.indexOf('oa_send_documents') >= 0){
		// 发文申请
		uni.navigateTo({
			url:'/pages/process/sendDocuments/detial?id='+id
		})
	}else if(str && str.indexOf('oa_application_number') >= 0){
		// 发文申请
		uni.navigateTo({
			url:'/pages/process/applicationNumber/detial?id='+id
		})
	}
}